package com.qf.collection;

import java.util.Collection;
import java.util.Iterator;

/**
 * 1. 自定义集合，实现Collection接口，要求底层使用链表存放集合元素
 */
public class MyCollection02 implements Collection {

    /**
     * 链表单元
     */
    class Obj {
        Object object; // 每个单元存储的内容
        Obj next;    // 指向链表中下一个单元

        public Obj() {
        }

        public Obj(Object object) {
            this.object = object;
        }

        public Object getObject() {
            return object;
        }

        public void setObject(Object object) {
            this.object = object;
        }

        public Obj getNext() {
            return next;
        }

        public void setNext(Obj next) {
            this.next = next;
        }
    }

    private int count = 0; // 单元个数
    private Obj object; // 一个单元
    private Obj index; // 指针

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public Obj getObject() {
        return object;
    }

    public void setObj(Obj object) {
        this.object = object;
    }

    public Obj getIndex() {
        return index;
    }

    public void setIndex(Obj index) {
        this.index = index;
    }

    @Override
    public int size() {
        return count;
    }

    @Override
    public boolean isEmpty() {

        if (object == null) return true;

        return false;
    }

    @Override
    public boolean contains(Object o) {
        Obj index = object;
        for (int i = 0; i < count; i++) {
            if (index.getObject().equals(o)) {
                return true;
            }

            index = index.getNext();
        }

        return false;

    }

    @Override
    public Iterator iterator() {
        return new Iterator() {

            int index = 0;
            Obj index_;

            @Override
            public boolean hasNext() {
                return index < count;
            }

            @Override
            public Object next() {
                index++;
                if (index_ == null) index_ = object;
                else {
                    index_ = index_.getNext();
                }
                return index_.getObject();
            }
        };


    }

    @Override
    public Object[] toArray() {
        Object[] o = new Object[count];

        Obj index = object;

        for (int i = 0; i < count; i++) {
            o[i] = index.getObject();
            index = index.getNext();
        }


        return o;
    }

    @Override
    public boolean add(Object o) {
        Obj newObj = new Obj(o);

        if (object == null) { // 如果链表为空,一个单元没有
            object = newObj; // 第一个单元赋值
        } else {
            index.setNext(newObj); // 否则根据指针放置新单元
        }
        index = newObj; // 更新指针,指向新放入的单元,即为最后一个单元

        count++; // 计数加一

        return true;
    }

    @Override
    public boolean remove(Object o) {

        if (object.getObject().equals(o)) { // 如果第一个单元需要移除
            object = object.getNext(); // 第一个单元指向下一个单元

            count--; // 个数减一
            return true;
        }

        Obj index = object; // 指针
        Obj next = object.getNext(); // 指针下一个

        while (true) { // 遍历链表查询

            if (next == null) // 终止条件,整个链表都没有查到
                return false;

            if (next.getObject().equals(o)) {  // 如果找到,当前单元和下一个单元相同
                index.setNext(next.getNext());
                count--;
                return true;
            }

            index = index.getNext(); // 未找到指向下一个单元,继续查找
            next = next.getNext();
        }
    }

    @Override
    public boolean addAll(Collection c) {
        for (Object o : c) {
            if (!add(o)) {
                return false;
            }
        }

        return true;
    }

    @Override
    public void clear() {
        object = null;
        index = null;
    }

    @Override
    public boolean retainAll(Collection c) {
        MyCollection02 myCollection02 = new MyCollection02();
        for (Object o : c) {
            if (contains(o)) {
                myCollection02.add(o);
            }
        }

        object = myCollection02.object;

        if (myCollection02.object == null) {
            return false;
        }
        return true;
    }

    @Override
    public boolean removeAll(Collection c) {
        for (Object o : c) {
            if (!remove(o)) {
                return false;
            }
        }

        return true;
    }

    @Override
    public boolean containsAll(Collection c) {
        for (Object o : c) {
            if (!contains(o)) {
                return false;
            }
        }
        return true;
    }

    @Override
    public Object[] toArray(Object[] o) {
        Obj index = object;

        for (int i = 0; i < count; i++) {
            o[i] = index.getObject();
            index = index.getNext();
        }

        return o;
    }

    @Override
    public String toString() {
        if (object == null) {
            return "[]";
        }

        Obj index = object;
        String str = "[";
        for (int i = 0; i < count; i++) {

            if (index == null) break;

            str += index.getObject();
            if (index.getNext() == null) {
                str += "]";
            } else {
                str += " ";
            }

            index = index.getNext();
        }

        return str;
    }
}
